package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.core.domain.dto.RoleDTO;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjNWarningZlyjBo;
import com.ruoyi.zhgd.domain.vo.ZjNWarningZlyjVo;
import com.ruoyi.zhgd.domain.ZjNWarningZlyj;
import com.ruoyi.zhgd.mapper.ZjNWarningZlyjMapper;
import com.ruoyi.zhgd.service.IZjNWarningZlyjService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 张拉压浆预警信息Service业务层处理
 *
 * @author tll
 * @date 2023-12-04
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjNWarningZlyjServiceImpl implements IZjNWarningZlyjService {

    private final ZjNWarningZlyjMapper baseMapper;

    /**
     * 查询张拉压浆预警信息
     */
    @Override
    public ZjNWarningZlyjVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询张拉压浆预警信息列表
     */
    @Override
    public TableDataInfo<ZjNWarningZlyjVo> queryPageList(ZjNWarningZlyjBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjNWarningZlyj> lqw = buildQueryWrapper(bo);
        Page<ZjNWarningZlyjVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询张拉压浆预警信息列表
     */
    @Override
    public List<ZjNWarningZlyjVo> queryList(ZjNWarningZlyjBo bo) {
        LambdaQueryWrapper<ZjNWarningZlyj> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjNWarningZlyj> buildQueryWrapper(ZjNWarningZlyjBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjNWarningZlyj> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ZjNWarningZlyj::getType, bo.getType());
        lqw.eq(bo.getInsterTime() != null, ZjNWarningZlyj::getInsterTime, bo.getInsterTime());
        lqw.eq(bo.getBusinessId() != null, ZjNWarningZlyj::getBusinessId, bo.getBusinessId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ZjNWarningZlyj::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增张拉压浆预警信息
     */
    @Override
    public Boolean insertByBo(ZjNWarningZlyjBo bo) {
        ZjNWarningZlyj add = BeanUtil.toBean(bo, ZjNWarningZlyj.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改张拉压浆预警信息
     */
    @Override
    public Boolean updateByBo(ZjNWarningZlyjBo bo) {
        ZjNWarningZlyj update = BeanUtil.toBean(bo, ZjNWarningZlyj.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjNWarningZlyj entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除张拉压浆预警信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 张拉预警信息推送分页查询
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<ZjNWarningZlyjVo> pageTension(ZjNWarningZlyjBo bo, PageQuery pageQuery) {
        // 获取当前用户角色信息,如果是监理,则为待审批,如果非监理,则为未处置
        Long userId = LoginHelper.getUserId();
        Boolean contains = judgeIsJl(LoginHelper.getLoginUser().getRoles());
        if (contains) { // 监理 处置中
//            status = 3;
//            disposalStatus = 3;
            // 如果是监理,只能确认施工发给他的预警处置单,但是可以查看所有处置单的详情
        } else { // 非监理 未处置
//            status = 1;
//            disposalStatus = 1;
            // 如果是施工,只能撤回当前账号发起的处置单,但是可以查看所有处置单详情
        }
        Page<ZjNWarningZlyjVo> data = baseMapper.pageTension(pageQuery.build(), bo);
        return TableDataInfo.build(data);
    }

    /**
     * 压浆预警信息推送分页查询
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<ZjNWarningZlyjVo> pageGrouting(ZjNWarningZlyjBo bo, PageQuery pageQuery) {
        Page<ZjNWarningZlyjVo> data = baseMapper.pageGrouting(pageQuery.build(), bo);
        return TableDataInfo.build(data);
    }

    // 监理角色
    public final String[] jlArr = {"jw.1","jw.2","jw.3","jw.4"};

    /**
     *  判断是否监理角色
     * @param roles
     * @return
     */
    public Boolean judgeIsJl(List<RoleDTO> roles) {
        Boolean contains = false;
        if (CollectionUtils.isNotEmpty(roles)) {
            for (RoleDTO role : roles) {
                contains = Arrays.asList(jlArr).contains(role.getRoleKey());
            }
        }
        return contains;
    }
}
